<template>
  <TreeTableLayout>
    <template #tree>
      <Tree @query="doQuery" @gateway="getGateway" :gatewayType="1"></Tree>
    </template>
    <template #table>
      <RightTableLayout :slotName="['operation', 'table', 'pagination']">
        <template #operation>
          <div :class="{ btnbox: pid }">
            <div class="wireless-device-btn-box">
              <el-row>
                <el-col :span="24" :class="i18n.fy.灯控操作 + 'div'">
                  <span :span="4">
                    <span style="margin-right: 0.3rem;">{{ i18n.fy.灯控操作 }}</span>
                    <el-select style="width: 120px" v-model="area" class="m-2" placeholder="">
                      <el-option v-for="item in darrOptions" :key="item.value" :label="item.label"
                        :value="item.value" />
                    </el-select>
                  </span>
                  <span :span="3" v-if="area != ''">
                    <span>{{ i18n.fy.区 }}</span>
                    <el-select v-model="district" :placeholder="i18n.fy.区" style="width: 100px">
                      <el-option v-for="item in deviceAreaAllList" :value="item.area_code">
                        {{ item.area_code }}
                      </el-option>
                    </el-select>
                  </span>
                  <span :span="3" v-if="area != '' && area == '300'">
                    <span>
                      {{ i18n.language === "zh-cn" ? "标签" : "label" }}
                    </span>
                    <el-input style="width: 120px" v-model="tag" placeholder="" />
                  </span>
                  <span :span="3" v-if="area != '' && area == '200'">
                    <span> {{ i18n.language === "zh-cn" ? "组" : "group" }}</span>
                    <el-select v-model="group" placeholder="" style="width: 100px">
                      <el-option v-for="item in options" :value="item.group_name">{{ item.group_name }} </el-option>
                    </el-select>
                  </span>
                  <span :span="3" v-if="area != '' && area == '100'">
                    <span>{{ i18n.language === "zh-cn" ? "号" : "name" }}</span>
                    <el-input style="width: 120px" v-model="num" placeholder="" />
                  </span>
                  <span :span="6" style="margin-left: 0.66rem;">
                    <el-button type="primary" @click="Everbright('lightOn')">
                      {{ i18n.language === "zh-cn" ? "常亮" : "Light" }}
                    </el-button>
                    <el-button type="primary" @click="extinction('lightOff')">
                      {{ i18n.language === "zh-cn" ? "常灭" : "ConstantExtinction" }}
                    </el-button>
                    <el-button type="primary" @click="flash('blink')">
                      {{ i18n.language === "zh-cn" ? "闪一闪" : "Twinkle" }}
                    </el-button>
                    <el-button type="primary" @click="stopFlash('stopBlink')">
                      {{ i18n.language === "zh-cn" ? "停止闪" : "Stop flashing" }}
                    </el-button>
                    <el-button type="primary" @click="dormancy('lightSleep')">
                      {{ i18n.language === "zh-cn" ? "休眠" : "hibernate" }}
                    </el-button>
                  </span>
                </el-col>
              </el-row>
            </div>
          </div>
        </template>
        <template #table>
          <el-table :empty-text="i18n.fy.nodata" class="wiretab" @select="selectClick" ref="multipleTableRef"
            :data="data.tableData" border v-loading="data.loading" :element-loading-text="i18n.fy.loading"
            @selection-change="handleSelectionChange" @row-click="handleRowClick" @sort-change="sort_change">
            <el-table-column type="selection" width="55" fixed :selectable="selected" :style="{ width: '55px' }" />
            <el-table-column prop="macaddress" :label="i18n.fy.SerialNumber" width="70" align="center" fixed
              :style="{ width: '70px' }">
              <template #default="scope">
                {{ scope.$index + 1 + (data.queryForm.page - 1) * data.queryForm.limit }}
              </template>
            </el-table-column>
            <el-table-column prop="macaddress" :label="i18n.fy.MACAddress" width="150" fixed
              :style="{ width: '150px' }" />
            <el-table-column prop="customName" :label="i18n.fy.DeviceAliases" :sortable="true" fixed width="150"
              :style="{ width: '150px' }" :show-overflow-tooltip="true">
              <template #default="scope">
                <span v-if="scope.row.customName === '01'">CCO</span>
                <span v-else-if="scope.row.customName === '02'">STA</span>
                <span v-else-if="scope.row.customName === '03'">METER</span>
                <span v-else>
                  {{ scope.row.customName || "" }}
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="deviceType" :label="i18n.fy.EquipmentName" :sortable="true" width="150" />
            <el-table-column prop="operatingStatus" :label="i18n.fy.DeviceStatus" :sortable="true" width="150">
              <template #default="scope">
                <span v-if="scope.row.operatingStatus === '01'" style="color: lawngreen">在线</span>
                <span v-else-if="scope.row.operatingStatus === '00'" style="color: #ddd">离线</span>
                <span v-else>{{ scope.row.operatingStatus }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="areaCode" :label="i18n.fy.EquipmentArea" width="150" sortable="custom">
            </el-table-column>
            <el-table-column prop="groupName" :label="i18n.fy.EquipmentGroupNumber" width="230">
              <template #header="scope">
                <div style="display: flex" class="selectName">
                  <span>{{ i18n.fy.EquipmentGroupNumber }}</span>
                  <select v-model="customName" placeholder="" @change="setCustomName()">
                    <option v-for="item in options" :value="item.v">{{ item.group_name }}</option>
                  </select>
                </div>
              </template>
              <template #default="scope">
                <span>
                  {{ scope.row.groupName }}
                </span>
              </template>
            </el-table-column>
            <el-table-column prop="luminance" :label="i18n.fy.CurrentBrightness" width="150"> </el-table-column>
            <el-table-column prop="triggerOpenLightNum" :label="i18n.fy.Someonebrightness" width="150">
            </el-table-column>
            <el-table-column prop="triggerFloodlightNum" :label="i18n.fy.Unmannedbrightness" width="150">
            </el-table-column>
            <el-table-column prop="darkenDelay" :label="i18n.fy.Delaytime" width="150"> </el-table-column>
            <el-table-column prop="colorTemperature" :label="i18n.fy.Currentchromaticity" width="150">
            </el-table-column>
            <el-table-column prop="hasTrigger" :label="i18n.fy.Sensingheadstatus" width="150"> </el-table-column>
            <el-table-column prop="sensorSensitivity" :label="i18n.fy.sensorSensitivity" width="150"> </el-table-column>
            <el-table-column prop="sceneNo" :label="'情景模式'" width="150">
              <template #default="scope">
                {{ scope.row.sceneNo }}
              </template>
            </el-table-column>
            <el-table-column prop="ratedPower" :label="i18n.fy.ratedPower" width="150"> </el-table-column>
            <el-table-column prop="energyDur" :label="i18n.fy.electricity + '(KWH)'" width="150">
              <template #default="scope">
                <span v-if="scope.row.energyDur != null">
                  {{ ((((scope.row.ratedPower * 1) / 1000) * scope.row.energyDur * 1) / 3600).toFixed(3) }}
                </span>
                <span v-if="scope.row.energyDur == null"> </span>
              </template>
            </el-table-column>
            <el-table-column prop="amp" :label="i18n.fy.power + '(KW)'" width="150">
              <template #default="scope">
                <span v-if="scope.row.amp != null"> {{ scope.row.amp / 100 }}</span>
                <span v-if="scope.row.amp == null"> </span>
              </template>
            </el-table-column>
            <el-table-column prop="watt" :label="i18n.fy.current + '(A)'" width="150">
              <template #default="scope">
                <span v-if="scope.row.watt != null">{{ scope.row.watt / 100 }}</span>
                <span v-if="scope.row.watt == null"> </span>
              </template>
            </el-table-column>
            <el-table-column prop="softwareVersion" :label="i18n.fy.Version" width="150" />
            <el-table-column prop="updateTime" :label="i18n.fy.updateTime" width="170"> </el-table-column>
          </el-table>
        </template>
        <template #pagination>
          <el-pagination @size-change="onChanSize" @current-change="onChanPage"
            v-model:current-page="data.queryForm.page" v-model:page-size="data.queryForm.limit" :total="data.total" />
        </template>
      </RightTableLayout>
    </template>
  </TreeTableLayout>
  <!-- 弹窗 -->
  <div id="slideDiv" style="display: none; padding: 20px; text-align: center">
    <div id="slideTest1"></div>
  </div>
  <div id="slideDiv2" style="display: none; padding: 20px; text-align: center">
    <div class="layui-form-item">
      <label class="layui-form-label">关灯值:</label>
      <div class="layui-input-block">
        <div id="slide1"></div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">开灯值:</label>
      <div class="layui-input-block">
        <div id="slide2"></div>
      </div>
    </div>
  </div>
  <div id="slideDiv3" class="slideDiv linev" style="display: none; padding: 20px; text-align: center">
    <div class="layui-form-item">
      <label class="layui-form-label">亮度调高时间:</label>
      <div class="layui-input-block">
        <div id="slide3"></div>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">亮度调低时间:</label>
      <div class="layui-input-block">
        <div id="slide4"></div>
      </div>
    </div>
  </div>
  <div id="lightTestDiv5" style="display: none; padding: 20px; text-align: center">
    <form class="layui-form" action="">
      <div class="layui-form-item">
        <label class="layui-form-label">模式：</label>
        <div class="layui-input-block radiocont">
          <el-radio-group v-model="radio4" class="ml-4">
            <el-radio :label="i18n.fy.zdgx" :value="'reportSetting'" size="large"></el-radio>
            <el-radio :label="i18n.fy.sdgx" :value="'reportSettingAck'" size="large"></el-radio>
          </el-radio-group>
        </div>
      </div>
    </form>
  </div>
  <div id="lightTestDiv" style="display: none; padding: 20px; text-align: center">
    <form class="layui-form" action="">
      <div class="layui-form-item">
        <label class="layui-form-label">开关值:</label>
        <div class="layui-input-block">
          <input type="radio" name="lTest" value="0" title="开" checked="" />
          <div class="layui-unselect layui-form-radio layui-form-radioed">
            <i class="layui-anim layui-icon"></i>
            <div>开</div>
          </div>
          <input type="radio" name="lTest" value="1" title="关" />
          <div class="layui-unselect layui-form-radio">
            <i class="layui-anim layui-icon"></i>
            <div>关</div>
          </div>
        </div>
      </div>
    </form>
  </div>
  <div id="lightNameDiv" style="display: none; padding: 20px; text-align: center">
    <form class="layui-form" action="">
      <div class="layui-form-item">
        <div class="layui-inline">
          <div class="lightNameDiv">
            <label class="layui-form-label">设备名称:</label>
            <div class="layui-input-inline">
              <input type="text" name="customName" id="customName" autocomplete="off" class="layui-input" />
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
  <div id="groupNameDiv" style="display: none; padding: 20px">
    <div class="layui-form-item">
      <div class="layui-form-item">
        <label class="layui-form-label">组名:</label>
        <div class="layui-input-block">
          <input type="text" name="groupName" id="groupName" lay-verify="required" autocomplete="off"
            class="layui-input" />
        </div>
      </div>
    </div>
  </div>
  <div id="lightNameDiv2" style="display: none; padding: 20px; text-align: center">
    <form class="layui-form" action="">
      <div class="layui-form-item">
        <div class="layui-inline">
          <div>
            <label class="layui-form-label">设备组号:</label>
            <div class="layui-input-inline">
              <input type="text" name="groupNum" id="groupNum" autocomplete="off" class="layui-input" />
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
  <div id="lightNameDiv4" style="display: none; padding: 20px; text-align: center">
    <form class="layui-form" action="">
      <div class="layui-form-item">
        <div class="layui-inline">
          <div>
            <label class="layui-form-label">设备区号:</label>
            <div class="layui-input-inline">
              <input type="text" name="areaCode" id="areaCode" autocomplete="off" class="layui-input" />
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>

  <el-dialog v-model="dialogVisible" :title="i18n.fy.setSB_ma" width="40%">
    <div>
      <div class="top">
        <el-row>
          <span :span="4">
            <el-select v-model="area" class="m-2" placeholder="">
              <el-option v-for="item in darrOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </span>
          <span :span="5" v-if="area != ''">
            <span>
              {{ i18n.language === "zh-cn" ? "网关" : "gateway" }}
            </span>
            <el-select v-model="gateway" class="m-2" placeholder="">
              <el-option :label="'全部网关'" :value="''" />
              <el-option v-for="item in gatewayList" :key="item.id" :label="item.title" :value="item.id" />
            </el-select>
          </span>
          <span :span="4" v-if="area != ''">
            <span>{{ i18n.language === "zh-cn" ? "区" : "distinguish" }}</span>
            <el-input v-model="district" placeholder="区" />
          </span>
          <span :span="4" v-if="area != '' && area == '300'">
            <span>{{ i18n.language === "zh-cn" ? "标签" : "label" }}</span>
            <el-input v-model="tag" placeholder="" />
          </span>
          <span :span="4" v-if="area != '' && area == '200'">
            <span>{{ i18n.language === "zh-cn" ? "组" : "group" }}</span>
            <el-select v-model="group" placeholder="" style="width: 100px">
              <el-option v-for="item in options" :value="item.group_name">{{ item.name || item.group_name }}</el-option>
            </el-select>
          </span>
          <span :span="4" v-if="area != '' && area == '100'">
            <span>{{ i18n.language === "zh-cn" ? "号" : "name" }}</span>
            <el-input v-model="num" placeholder="" />
          </span>
        </el-row>
      </div>
      <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
        <el-tab-pane :label="i18n.fy.lampsettings" name="first">
          <el-tabs :tab-position="'left'" type="border-card" style="height: 700px" class="demo-tabs">
            <el-tab-pane :label="i18n.fy.basicsettings">
              <el-row>
                <el-col :span="4"> 亮灯模式 </el-col>
                <el-col :span="14">
                  <el-radio-group v-model="radio1" size="large">
                    <el-radio-button label="常亮" />
                    <el-radio-button label="常灭" />
                    <el-radio-button label="感应" />
                  </el-radio-group>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setLightMode', radio1)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 有人亮度 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue" /><span>{{ sliderValue }}%</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setHighBright', sliderValue)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 无人亮度 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue2" /><span>{{ sliderValue2 }}%</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setStandbyBright', sliderValue2)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 色温 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue3" /><span>{{ sliderValue3 }}%</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setCctBright', sliderValue3)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 感应模式 </el-col>
                <el-col :span="14">
                  <el-radio-group v-model="radio2" size="large">
                    <el-radio-button label="一段" />
                    <el-radio-button label="二段" />
                  </el-radio-group>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setDelayMode', radio2)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 一段延时 </el-col>
                <el-col :span="14">
                  <el-select v-model="oneDate" class="m-2" placeholder="" size="large">
                    <el-option v-for="item in dateOptions" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setDelayTime', oneDate)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 二段延时 </el-col>
                <el-col :span="14">
                  <el-select v-model="one2Date" class="m-2" placeholder="" size="large">
                    <el-option v-for="item in dateOptions" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setDelayTime2', one2Date)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 恒照度模式 </el-col>
                <el-col :span="14">
                  <el-radio-group v-model="radio3" size="large">
                    <el-radio-button label="无效" />
                    <el-radio-button label="自控" />
                    <el-radio-button label="被控" />
                  </el-radio-group>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setAlsMode', radio3)">发送</el-button>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="传感器设置">
              <el-row>
                <el-col :span="4"> 传感器开关 </el-col>
                <el-col :span="14">
                  <el-switch v-model="switchValue" size="large" active-text="开" inactive-text="关" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send(switchValue ? 'ssrOn' : 'ssrOff', '')">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 网组开关 </el-col>
                <el-col :span="14">
                  <el-switch v-model="switchValue2" size="large" active-text="开" inactive-text="关" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send(switchValue2 ? 'ssrOn' : 'netOff', '')">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 触发间隔 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue4" :max="9" /><span>{{ sliderValue4 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setSensorInterval', sliderValue4)">发送</el-button>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="调光设置">
              <el-row>
                <el-col :span="4"> 亮灯速度 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue5" :max="9" /><span>{{ sliderValue5 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setBrightRiseTime', sliderValue5)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 灭灯速度 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue6" :max="9" /><span>{{ sliderValue6 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setBrightFallTime', sliderValue6)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 色温调高速度 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue7" :max="9" /><span>{{ sliderValue7 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setCctRiseTime', sliderValue7)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 色温降低速度 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue8" :max="9" /><span>{{ sliderValue8 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setCctFallTime', sliderValue8)">发送</el-button>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="遥控器设置">
              <el-row>
                <el-col :span="4"> 红外遥控 </el-col>
                <el-col :span="14">
                  <el-switch v-model="switchValue3" size="large" active-text="开" inactive-text="关" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send(switchValue3 ? 'ircOn' : 'ircOff', '')">发送</el-button>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="恒照度">
              <el-row>
                <el-col :span="4"> 设置照度值 </el-col>
                <el-col :span="14">
                  <el-input v-model="input1" placeholder="目标照度值[0~9000]" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setAlsLux', input1)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 发射开关 </el-col>
                <el-col :span="14">
                  <el-switch v-model="switchValue4" size="large" active-text="开" inactive-text="关" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary"
                    @click="send(switchValue4 ? 'alsSyncDisable' : 'alsSyncEnable', '')">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 发射间隔 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue9" :max="9" /><span>{{ sliderValue9 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setAlsInterval', sliderValue9)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 给恒照度传感器起名 </el-col>
                <el-col :span="14">
                  <el-input v-model="input2" placeholder="当前照度昵称" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setAlsNotice', input2)">发送</el-button>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="网络设置">
              <el-row>
                <el-col :span="4"> 组跳数 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue10" :max="9" /><span>{{ sliderValue10 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setGroupTtl', sliderValue10)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 网跳数 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue11" :max="9" /><span>{{ sliderValue11 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setNetTtl', sliderValue11)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 重发次数 </el-col>
                <el-col :span="14">
                  <div class="sliderbox">
                    <el-slider v-model="sliderValue12" :max="9" /><span>{{ sliderValue12 }}秒</span>
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setTxTimes', sliderValue12)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 转发开关 </el-col>
                <el-col :span="14">
                  <el-switch v-model="switchValue5" size="large" active-text="开" inactive-text="关" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send(switchValue5 ? 'rlyOff' : 'rlyOn', '')">发送</el-button>
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
        <el-tab-pane label="设备编号" name="second">
          <el-tabs :tab-position="'left'" type="border-card" style="height: 700px" class="demo-tabs">
            <el-tab-pane label="编号设置">
              <el-row>
                <el-col :span="4"> 修改区 </el-col>
                <el-col :span="14">
                  <el-input v-model="input3" placeholder="请输入新区" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setAreaAddress', input3)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 修改组 </el-col>
                <el-col :span="14">
                  <el-input v-model="input4" placeholder="请输入新区" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setClusterAddress', input4)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 修改号 </el-col>
                <el-col :span="14">
                  <div style="display: flex">
                    <el-input v-model="input5" placeholder="请输入UUID" style="margin-right: 10px" />
                    <el-input v-model="input6" placeholder="请输入新号" />
                  </div>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setNumberAddress', input5 + '_' + input6)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 设置标签 </el-col>
                <el-col :span="14">
                  <el-input v-model="input7" placeholder="请输入新标签" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setLabel', input7)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 删除标签 </el-col>
                <el-col :span="14">
                  <el-input v-model="input8" placeholder="请输入标签名" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('deleteLabel', input8)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 清空标签 </el-col>
                <el-col :span="14"> </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('clearLabel', '')">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 设置邻组通知 </el-col>
                <el-col :span="14">
                  <el-input v-model="input9" placeholder="请输入邻组组号" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setNeighbour', input9)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 删除邻组通知 </el-col>
                <el-col :span="14">
                  <el-input v-model="input10" placeholder="请输入邻组组号" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('deleteNeighbour', '')">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 清空邻组通知 </el-col>
                <el-col :span="14"> </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('clearNeighbour', '')">发送</el-button>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="设备关联">
              <el-row>
                <el-col :span="4"> 设备命名 </el-col>
                <el-col :span="14">
                  <el-input v-model="input11" placeholder="请输入设备名称" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('setNickName', input11)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 关联设备 </el-col>
                <el-col :span="14">
                  <el-input v-model="input12" placeholder="请输入设备名称" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('bind', input12)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 解除关联 </el-col>
                <el-col :span="14">
                  <el-input v-model="input13" placeholder="请输入设备名称" />
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('relieve', input13)">发送</el-button>
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
        <el-tab-pane label="情景模式" name="third">
          <el-tabs :tab-position="'left'" type="border-card" style="height: 700px" class="demo-tabs">
            <el-tab-pane label="常规情景">
              <el-row>
                <el-col :span="4"> 读取情景模式 </el-col>
                <el-col :span="14"> </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('readScene', '1')">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="4"> 切换情景模式 </el-col>
                <el-col :span="14">
                  <el-select v-model="scene" class="m-2" placeholder="请选择情景模式" size="large">
                    <el-option v-for="item in sceneOptions" :key="item.value" :label="item.label" :value="item.value" />
                  </el-select>
                </el-col>
                <el-col :span="4" style="text-align: right">
                  <el-button type="primary" @click="send('callScene', scene)">发送</el-button>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="24">
                  <p style="color: rgb(217, 150, 0); font-size: 14px">重命名可作用于单灯、群组、区操作等</p>
                </el-col>
              </el-row>
            </el-tab-pane>
            <el-tab-pane label="定时情景">
              <el-row v-show="sceneVisible">
                <el-col :span="4"> 定时情景列表 </el-col>
                <el-col :span="20" style="text-align: right">
                  <el-button type="primary" @click="addScene()">新增定时情景</el-button>
                </el-col>
              </el-row>
              <el-row v-show="sceneVisible" style="border-bottom: 0">
                <el-col :span="24">
                  <div>
                    <el-table :empty-text="i18n.fy.nodata" :data="sceneTableData" style="width: 100%; height: 600px"
                      class="sceneTableData">
                      <el-table-column prop="ruleTitle" label="任务名称" />
                      <el-table-column prop="startTime" label="执行时间" width="120">
                        <template #default="scope"> {{ showTime(scope.row) }}{{ scope.row.startTime }} </template>
                      </el-table-column>
                      <el-table-column prop="name" label="调用情景" width="120">
                        <template #default="scope">
                          {{ showScope(scope.row) }}
                        </template>
                      </el-table-column>
                      <el-table-column prop="name" label="灯组" width="180">
                        <template #default="scope">
                          <p class="operateInfo" v-for="(item, i) in showOper(scope.row.operateInfo)">
                            {{ item.area }}区
                            <span v-if="item.code != '400'">{{ item.address }}</span>
                            <span v-if="item.code == '200'">组</span>
                            <span v-if="item.code == '100'">号</span>
                          </p>
                        </template>
                      </el-table-column>
                      <el-table-column prop="name" label="操作" width="210">
                        <template #default="scope">
                          <el-button link type="primary" @click="editScen(scope.row)">编辑</el-button>
                          <el-button link type="primary" @click="changeEnableState(scope.row, 1)"
                            v-if="scope.row.enableState == '0'">启用</el-button>
                          <el-button link type="danger" @click="changeEnableState(scope.row, 0)"
                            v-if="scope.row.enableState == '1'">停用</el-button>
                          <el-button link type="danger" @click="delScen(scope.row)">删除</el-button>
                        </template>
                      </el-table-column>
                    </el-table>
                  </div>
                </el-col>
              </el-row>
              <el-row v-show="!sceneVisible">
                <div class="sceneVisible">
                  <el-col :span="24">
                    <div class="newName">
                      <span>任务名称</span>
                      <el-input v-model="scenModel.data.ruleTitle" placeholder="请输入" style="width: 220px" />
                    </div>
                  </el-col>
                  <el-col :span="24">
                    <div class="newName">
                      <span>执行模式</span>
                      <el-select style="width: 220px" v-model="scenModel.data.chooseWorkDay" class="m-2"
                        placeholder="请选择情景模式">
                        <el-option v-for="item in carryOptions" :key="item.value" :label="item.label"
                          :value="item.value" />
                      </el-select>
                      <span>执行时间</span>
                      <el-time-picker style="width: 220px" format="HH:mm" data-format="HH:mm" value-format="HH:mm"
                        v-model="scenModel.data.startTime" placeholder="任意时间点" />
                    </div>
                  </el-col>
                  <el-col :span="24">
                    <div class="newName">
                      <span>调用情景</span>
                      <el-select style="width: 220px" v-model="transferScene" class="m-2" placeholder="">
                        <el-option v-for="item in sceneOptions" :key="item.value" :label="item.label"
                          :value="item.value" />
                      </el-select>
                    </div>
                  </el-col>
                  <el-col :span="24">
                    <div class="newName">
                      <span>操控灯组</span>
                    </div>
                  </el-col>
                  <el-col :span="24">
                    <p class="altconp">
                      <span class="altcon" v-for="(item, i) in scenModel.data.operateInfo" @click="deleteTag(i)">
                        {{ item.area }}区
                        <span v-if="item.code != '400'">{{ item.address }}</span>
                        <span v-if="item.code == '200'">组</span>
                        <span v-if="item.code == '100'">号</span>
                      </span>
                      <el-button style="margin-left: 5px" type="primary" @click="addingLamp()">新增灯</el-button>
                    </p>
                  </el-col>
                  <el-col :span="24" style="text-align: center">
                    <el-button type="primary" @click="submitScen()">提交定时情景</el-button>
                    <el-button type="" @click="sceneVisible = true">返回</el-button>
                  </el-col>
                </div>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
      </el-tabs>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
      </span>
    </template>
  </el-dialog>
  <el-dialog v-model="lampVisible" draggable title="新增灯具" width="40%" class="">
    <div style="padding-bottom: 30px">
      <div class="top">
        <el-row>
          <span :span="3">
            <el-select style="width: 120px" v-model="typeOpt" class="m-2" placeholder="">
              <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </span>
          <span :span="4" v-if="typeOpt != 10">
            <span>网关</span>
            <el-select v-model="gateway_1" class="m-2" placeholder="">
              <el-option :label="'全部网关'" :value="''" />
              <el-option v-for="item in gatewayList" :key="item.id" :label="item.title" :value="item.id" />
            </el-select>
          </span>
          <span :span="4" v-if="typeOpt != 10">
            <span>区</span>
            <el-input v-model="district_1" placeholder="区" />
          </span>
          <span :span="4" v-if="typeOpt == 200 && typeOpt != 10">
            <span>组</span>
            <!-- <el-input v-model="group_1" placeholder="" /> -->
            <el-select v-model="group_1" placeholder="" style="width: 100px">
              <el-option v-for="item in options" :value="item.group_name">{{ item.name || item.group_name }}</el-option>
            </el-select>
          </span>
          <span :span="4" v-if="typeOpt == 100 && typeOpt != 10">
            <span>号</span>
            <el-input v-model="num_1" placeholder="" />
          </span>
          <span :span="6" v-if="typeOpt != 10"> <span>网络密码</span><el-input style="width: 150px" v-model="identity_1"
              placeholder="网络密码" /> </span>
        </el-row>
      </div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="enterOs()">确认</el-button>
        <el-button @click="lampVisible = false"> 取消 </el-button>
      </span>
    </template>
  </el-dialog>
  <el-dialog v-model="dialogVisible2" draggable title="扫描添灯" :show-close="false" width="50%" class="newAdd"
    style="background: #000">
    <div style="padding-bottom: 30px">
      <el-table :empty-text="i18n.fy.nodata" :data="data.tableData2" height="600" v-loading="data.loading2"
        element-loading-text="正在扫描中....." style="width: 100%" class="newAddTab"
        @selection-change="handleSelectionChange2">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="macaddress" label="MAC地址" width="180" />
        <el-table-column prop="areaCode" label="区" sortable="true" />
        <el-table-column prop="groupName" label="组" sortable="true" />
        <el-table-column prop="deviceIndex" label="号" />
        <el-table-column prop="deviceType" label="类型" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column prop="option" label="操作" width="120">
          <template #default="scope">
            <el-button type="primary" size="small" @click="start(scope.row)">闪一闪</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="operateScan('scan')">扫描设备</el-button>
        <el-button type="primary" @click="operateStop()">停止扫描</el-button>
        <el-button type="primary" @click="enterAdd()">确认新增</el-button>
        <el-button @click="close"> 关闭 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
defineOptions({ name: "WirelessDevice" });
import { i18nStore } from "@/pinia/i18n";
const i18n = i18nStore();
import { workbenchScreenStore } from "@/pinia/workbenchScreen";
const wsStore = workbenchScreenStore();
import RightTableLayout from "@/views/workbench/components/RightTableLayout.vue";
import TreeTableLayout from "@/views/workbench/components/TreeTableLayout.vue";
import Tree from "@/components/tree.vue";
const {
  appContext: {
    config: { globalProperties },
  },
} = getCurrentInstance();

let radio1 = ref("常亮");
let radio2 = ref("一段");
let radio3 = ref("无效");
let radio4 = ref("reportSetting");

let $httpUrl = globalProperties.$httpUrl;
let data = reactive({
  queryForm: {
    page: 1,
    limit: 100,
  },
  tableData: [],
  tableData2: [],
  total: 0,
  dialog: false,
  form: {},
  rules: {
    name: [{ required: true, message: "请输入姓名", trigger: "change" }],
    role: [{ required: true, message: "请选择岗位", trigger: "change" }],
  },
});
let pid = ref("");
let queryParams = {};
const doQuery = (params) => {
  customName.value = "全部";
  pid.value = params.deviceFid;
  gateway.value = pid.value;
  queryParams = params;
  getDeviceAliasAllList();
  getDeviceAreaAllList();
  getList(params);
  $.get($httpUrl + "/userLightNum/queryUserLightNum", { deviceFid: pid.value }, function (res) {
    if (res.data != null) {
      lightOnNum = res.data.lightOnNum;
      lightOffNum = res.data.lightOffNum;
    } else {
      lightOnNum = 40;
      lightOffNum = 0;
    }
  });
};
let gatewayList = ref([]);
const getGateway = (params) => {
  gatewayList.value = params;
};
const activeName = ref("first");
const handleClick = (tab, event) => { };

let deviceAreaAllList = ref([]);
const arrDistinctByProp = (arr, prop) => {
  let obj = {};
  return arr.reduce(function (preValue, item) {
    obj[item[prop]] ? "" : (obj[item[prop]] = true && preValue.push(item));
    return preValue;
  }, []);
};

const getDeviceAreaAllList = () => {
  var formData = new FormData(); // 当前为空
  formData.append("deviceFid", pid.value);
  $axios
    .post("/operationDevice/deviceAreaAllList", formData)
    .then((res) => {
      let newArr = arrDistinctByProp(res.data.data, "area_code");
      deviceAreaAllList.value = newArr;
      deviceAreaAllList.value = deviceAreaAllList.value.sort((a, b) => {
        return a.area_code - b.area_code;
      });
    })
    .catch((err) => { });
};
const hex2int = (hex) => {
  if (hex == null || hex == "") {
    return;
  }
  if (hex == null || hex == "") {
    return;
  }
  var len = hex.length,
    a = new Array(len),
    code;
  for (var i = 0; i < len; i++) {
    code = hex.charCodeAt(i);
    if (48 <= code && code < 58) {
      code -= 48;
    } else {
      code = (code & 0xdf) - 65 + 10;
    }
    a[i] = code;
  }

  return a.reduce(function (acc, c) {
    acc = 16 * acc + c;
    return acc;
  }, 0);
};
const reQueryForm = () => {
  // 重置查询表单
  data.queryForm = {
    page: 1,
    limit: 100,
  };
};
const multipleTableRef = ref();
const multipleSelection = ref([]);
const multipleSelection2 = ref([]);
// const toggleSelection = (rows) => {
//   if (rows) {
//     rows.forEach((row) => {
//       multipleTableRef.value.toggleRowSelection(row, undefined)
//     })
//   } else {
//     multipleTableRef.value.clearSelection()
//   }
// }
const selected = (row, index) => {
  if (area.value == 100) {
    return true; //不可勾选
  } else {
    return false;
  }
};
// 控制单选——table选择项发生变化时
const selectClick = (selection, row) => {
  if (selection.length > 0) {
    num.value = selection[0].deviceIndex;
  } else {
    num.value = "";
  }
  if (selection.length > 1 && area.value == 100) {
    let del_row = selection.shift();
    multipleTableRef.value.toggleRowSelection(del_row, false); // 用于多选表格，切换某一行的选中状态，如果使用了第二个参数，则是设置这一行选中与否（selected 为 true 则选中）
    num.value = del_row.deviceIndex;
  }
};

const compare = (property, order) => {
  return function (a, b) {
    if (order == "descending") {
      return b[property] - a[property];
    } else if (order == "ascending") {
      return a[property] - b[property];
    } else {
      return 0;
    }
  };
};
let recordsOld = [];
const sort_change = (column) => {
  let arr = JSON.parse(JSON.stringify(recordsOld));
  if (column.order != null) {
    arr = arr.sort(compare(column.prop, column.order));
  } else {
    arr = groupAndSort("groupNameLabel", "areaCodeLabel", arr);
    arr = arr.sort(createComprisonFunction("areaCodeLabel"));
  }

  data.queryForm.page = 1;
  data.tableData = arr.slice((data.queryForm.page - 1) * data.queryForm.limit, data.queryForm.page * data.queryForm.limit);
};
const handleRowClick = (row, column, event) => {
  multipleTableRef.value.clearSelection();
  if (area.value == 100) {
    multipleTableRef.value.toggleRowSelection(row);
    num.value = row.deviceIndex;
  }
};
const handleSelectionChange = (val) => {
  multipleSelection.value = val;
  let arr = multipleSelection.value.map((item) => {
    return item.deviceIndex;
  });
  num.value = arr.splice(",");
  if (multipleSelection.value.length > 0) {
    district.value = multipleSelection.value[multipleSelection.value.length - 1].areaCode;
  }
};
const handleSelectionChange2 = (val) => {
  multipleSelection2.value = val;
};
const currentChange = (page) => {
  pageData.queryForm.page = page;
  getList(queryParams);
};

const sizeChange = (size) => {
  data.queryForm.page = size;
  getList(queryParams);
};

const add = () => {
  // 新增
};

const edit = (row) => {
  // 编辑
};
const onChanSize = (e) => {
  //修改分页数
  data.queryForm.page = 1;
  data.queryForm.limit = e;
  getList(queryParams);
};
const onChanPage = (e) => {
  //修改分页
  data.queryForm.page = e;
  getList(queryParams);
};

const del = (row) => {
  // 删除
  ElMessageBox.confirm("确认删除?", "提示").then(() => {
    let arr = JSON.parse(JSON.stringify([row]));
    for (let i = 0; i < arr.length; i++) {
      arr[i].action = 0;
    }
    let params = {
      deviceFid: pid.value,
      operationDevices: arr,
    };
    $axios.post("/operationDevice/updateBatch", params).then((res) => {
      if (res.data.code === 200) {
        layer.msg("删除成功！");
        layer.close(mainIndex);
        getList({ deviceFid: pid.value });
      } else {
        layer.msg("删除失败!");
        layer.close(mainIndex);
      }
    });
  });
};

let formRef = ref(null);

const submit = () => {
  // 提交新增/编辑表单
  formRef.value.validate((valid) => {
    if (valid) {
    }
  });
};

const getList = (params, num) => {
  // 获取表格数据
  data.loading = true;
  $axios
    .get("/operationDevice/queryOperationDeviceAllList", {
      ...data.queryForm,
      ...params,
      deviceFid: pid.value,
      gatewayType: 1,
      limit: 10000,
      page: 1,
    })
    .then((res) => {
      if (res.data.data == null) {
        // layer.msg(res.data.msg);
        data.loading = false;
        return;
      }
      for (let i = 0; i < res.data.data.records.length; i++) {
        if (res.data.data.records[i].areaCode) {
          res.data.data.records[i].areaCodeLabel = hex2int(res.data.data.records[i].areaCode.replace(/\s*/g, ""));
        }
        if (res.data.data.records[i].groupName) {
          res.data.data.records[i].groupNameLabel = hex2int(res.data.data.records[i].groupName.slice(1).replace(/\s*/g, ""));
        }
      }
      res.data.data.records = groupAndSort("groupNameLabel", "areaCodeLabel", res.data.data.records);
      res.data.data.records = res.data.data.records.sort(createComprisonFunction("areaCodeLabel"));
      recordsOld = JSON.parse(JSON.stringify(res.data.data.records));
      data.loading = false;
      data.tableData = res.data.data.records.slice((data.queryForm.page - 1) * data.queryForm.limit, data.queryForm.page * data.queryForm.limit);
      data.total = res.data.data.total;
    });
};

const showType = (type) => {
  if (type == -1) return "系统";
  if (type == 0) return "云平台";
  if (type == 1) return "云产品";
  if (type == 2) return "云设备";
};

//全局列表
var tableIns;
//后台设定开灯值
var lightOnNum = 40;
//后台设定关灯值
var lightOffNum = 0;
//全局弹出
var mainIndex;
var $ = "";
var layer = "";
var form = "";
var table = "";
var dtree = "";
var slider = "";
var slider1 = "";
var slider2 = "";
var dropdown = "";
var maxNum = "";
var setTimeouts = "";
var tipsTime = "";
//渲染滑块
var lightNum = 0;
let total = 0;
let loading;

const getWidth = () => {
  return window.innerWidth * 0.89 + "px";
};

const getHeight = () => {
  return window.innerHeight * 0.777 + "px";
};

let params = {
  deviceFid: pid.value,
  mesh: {
    code: "",
    deviceName: pid.value,
    area: "",
    address: "00 00",
    action: "blink",
    params: "",
    identity: "",
  },
};

let dialogVisible = ref(false);

const moreOperate = () => {
  dialogVisible.value = true;
  getTab2();
};

const getTab2 = () => {
  let params = { deviceFid: wsStore.gatewayId };
  $axios
    .get("/deviceTimingRule/findAllDeviceTimingRule?pageSize=1000&pageNum=1", params)
    .then((res) => {
      for (let i = 0; i < res.data.data.content.length; i++) {
        res.data.data.content[i].operateInfo = JSON.parse(res.data.data.content[i].operateInfo);
      }
      data.tableData = res.data.data.content;
      sceneTableData.value = res.data.data.content;
    })
    .catch((err) => { });
};
const operate = (action) => {
  params.mesh.code = area.value;
  params.mesh.area = district.value;
  if (area.value == 400) {
    params.mesh.address = district.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
  }
  if (area.value == 100) {
    params.mesh.address = num.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (num.value === "") {
      ElMessage.error("号不能为空");
      return;
    }
  }
  if (area.value == 200) {
    params.mesh.address = group.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (group.value === "") {
      ElMessage.error("组不能为空");
      return;
    }
  }
  if (area.value == 300) {
    params.mesh.address = tag.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (tag.value == "") {
      ElMessage.error("标签不能为空");
      return;
    }
  }
  layer.msg("正在请求中......");
  data.loading2 = true;
  params.mesh.action = action;
  params.deviceFid = pid.value;
  params.mesh.deviceName = pid.value;
  params.mesh.params = action == "lightOn" ? lightOnNum : lightOffNum;
  $axios
    .post("/operation/mesh", params)
    .then((res) => {
      layer.msg("请求成功!");
      data.loading2 = false;
    })
    .catch((err) => {
      layer.msg("请求失败!");
    });
};

//加载框
const showLoad = (msg, tipsTime = 0) => {
  return layer.msg(msg, {
    icon: 16,
    shade: [0.5, "#f5f5f5"],
    time: tipsTime,
    scrollbar: false,
    offset: "auto",
  });
};
const operationalType = (operationalType, num, allFlag) => {
  let typeList = ["开灯", "调灯", "关灯", "多灯测试开", "多灯测试关", "调整色温"];
  layer.msg(`正在请求中....`);
  for (let i = 0; i < multipleSelection.value.length; i++) {
    let item = multipleSelection.value[i];
    let macs = (item.deviceIndex * 1).toString(16).toUpperCase().toString();
    if (macs.length == 2) {
      macs = "00" + macs;
    }
    let address = macs[0] + macs[1] + " " + macs[2] + macs[3];
    let params = {
      deviceFid: pid.value,
      mesh: {
        code: 100,
        deviceName: pid.value,
        area: item.areaCode,
        address: address,
        action: operationalType,
        params: "",
        identity: "",
      },
    };
    $axios
      .post("/operation/mesh", params)
      .then((res) => {
        /// layer.msg("请求成功!");
        data.loading = false;
        if (res.data.code === 200) {
          layer.msg(`${typeList[num]}` + (i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully"));
        } else {
          showLoad(`${typeList[num]}请求发送失败`, 1000);
        }
      })
      .catch((err) => { });
  }
};
let options = ref([]);
let customName = ref(i18n.language === "zh-cn" ? "全部" : "allItems");

const uniqueFunc = (arr, uniId) => {
  const res = new Map();
  return arr.filter((item) => !res.has(item[uniId]) && res.set(item[uniId], 1));
};

const getDeviceAliasAllList = () => {
  if (!pid.value) {
    return;
  }
  $.post(
    $httpUrl + "/operationDevice/deviceGroupAllList",
    {
      deviceFid: pid.value,
    },
    function (res) {
      res.data.map((item) => {
        item.v = item.group_name;
      });
      options.value = uniqueFunc(res.data, "group_name");
      options.value.unshift({
        group_name: "全部",
        name: "全部",
        v: "全部",
      });
      options.value.sort((a, b) => {
        return a.group_name - b.group_name; // 从小到大 -> a.age - b.age
      });
    }
  );
};
const setCustomName = () => {
  let params = {};
  if (customName.value == (i18n.language === "zh-cn" ? "全部" : "allItems")) {
    params = { deviceFid: pid.value };
  } else {
    params = { groupName: customName.value, deviceFid: pid.value };
  }
  getList(params, 1);
  area.value = "200";
  let obj = options.value.filter((item) => {
    return item.v == customName.value;
  });
  group.value = obj[0].group_name;
};

const allLightOn = () => {
  layer.confirm("确定设备全部开灯？", function (index) {
    let params = {
      code: 400,
      deviceName: pid.value,
      area: "00 00",
      address: "00 00",
      action: "blink",
      params: "",
      identity: "",
    };
    $axios
      .post("/operation/mesh", params)
      .then((res) => {
        layer.msg("请求成功!");
        data.loading = false;
      })
      .catch((err) => {
        layer.msg("全设备开灯失败!");
      });
  });
};

const allLightAdjust = () => {
  params.mesh.code = area.value;
  params.mesh.area = district.value;
  if (area.value == 400) {
    params.mesh.address = district.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
  }
  if (area.value == 100) {
    params.mesh.address = num.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (num.value === "") {
      ElMessage.error("号不能为空");
      return;
    }
  }
  if (area.value == 200) {
    params.mesh.address = group.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (group.value === "") {
      ElMessage.error("组不能为空");
      return;
    }
  }
  if (area.value == 300) {
    params.mesh.address = tag.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (tag.value == "") {
      ElMessage.error("标签不能为空");
      return;
    }
  }
  var _sensitivity = 0;
  slider.render({
    elem: "#slideTest1", //绑定元素
    input: true, //输入框
    min: 0,
    change: function (value) {
      _sensitivity = value;
    },
  });

  mainIndex = layer.open({
    type: 1,
    title: "调整全设备亮度:",
    content: $("#slideDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      layer.close(mainIndex);
      loading = showLoad("全设备调整亮度请求中...");
      $axios
        .post("/operation/mesh", {
          deviceFid: pid.value,
          mesh: {
            code: 400,
            deviceName: pid.value,
            area: "00 00",
            address: "00 00",
            action: "setHighBright",
            params: _sensitivity,
            identity: "",
          },
        })
        .then((res) => {
          layer.msg("请求成功!");
          data.loading = false;
        })
        .catch((err) => {
          layer.msg("请求失败!");
        });
    },
    success: function (index) { },
  });
};

const allTrigger = () => {
  mainIndex = layer.open({
    type: 1,
    title: "全感应头开关",
    content: $("#lightTestDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      params.mesh.code = area.value;
      params.mesh.area = district.value;
      if (area.value == 400) {
        params.mesh.address = district.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
      }
      if (area.value == 100) {
        params.mesh.address = num.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (num.value === "") {
          ElMessage.error("号不能为空");
          return;
        }
      }
      if (area.value == 200) {
        params.mesh.address = group.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (group.value === "") {
          ElMessage.error("组不能为空");
          return;
        }
      }
      if (area.value == 300) {
        params.mesh.address = tag.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (tag.value == "") {
          ElMessage.error("标签不能为空");
          return;
        }
      }
      layer.close(mainIndex);
      const lTestState = $('input[name="lTest"]:checked').val();
      loading = showLoad("全感应头开关请求中...");
      let params = {
        deviceFid: 134,
        mesh: {
          code: 400,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: "",
          params: "",
          identity: "",
        },
      };
      let action = "";
      if (lTestState == "0") {
        params.mesh.action = "setLightMode";
        params.mesh.params = "感应";
      } else {
        params.mesh.action = "setLightMode";
        params.mesh.params = "常亮";
      }
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          res = res.data;
          if (res.code === 200) {
            loading = showLoad("全感应头开关请求成功！", 500);
            let reqNum = 1;
            /// loading = showLoad("等待感应头开关第" + reqNum + "次响应...");
            var timer = setInterval(function () {
              let code = JSON.parse(res.data).requestId;
            }, 2000);
          } else {
            layer.msg("感应头开关失败!");
          }
          data.loading = false;
        })
        .catch((err) => {
          layer.msg("请求失败!");
        });
    },
    success: function (index) { },
  });
};

const operateStop = () => {
  data.loading2 = false;
  window.clearInterval(timer);
};

const start = (item) => {
  let macs = (item.deviceIndex * 1).toString(16).toUpperCase().toString();
  if (macs.length == 2) {
    macs = "00" + macs;
  }
  let address = macs[0] + macs[1] + " " + macs[2] + macs[3];
  layer.msg("闪一闪请求成功!");
  let params = {
    deviceFid: pid.value,
    mesh: {
      code: 100,
      deviceName: pid.value,
      area: item.areaCode,
      address: address,
      action: "blink",
      params: "",
      identity: "",
    },
  };
  $axios
    .post("/operation/mesh", params)
    .then((res) => {
      if (res.data.code === 200) {
        layer.msg(`闪一闪请求发送成功`);
      } else {
        showLoad(`闪一闪请求发送失败`, 1000);
      }
    })
    .catch((err) => { });
};
const operateScan = () => {
  data.loading2 = true;
  window.clearInterval(timer);
  timer = window.setInterval(function () {
    $axios
      .post("/operationDevice/scan", {
        deviceFid: pid.value,
      })
      .then((res) => {
        data.tableData2 = res.data.data;
      });
  }, 1000);
};
let district_1 = ref("");
let group_1 = ref("");
let num_1 = ref("");
let tagList = ref([]);
let tags = ref([]);

const enterOs = () => {
  let address = "";
  if (typeOpt.value == "400") {
    address = district_1.value;
    if (typeOpt.value == "0" && district_1.value == "") {
      ElMessage.error("区组号信息不能为空");
      return;
    } else {
      tags.value.push(district_1.value + "区");
      scenModel.data.operateInfo.push({
        code: typeOpt.value,
        deviceName: pid.value,
        area: district_1.value,
        address: address,
        action: "callScene",
        params: transferScene.value,
        identity: identity_1.value,
      });
      lampVisible.value = false;
    }
  } else if (typeOpt.value == "200") {
    address = group_1.value;
    if (typeOpt.value == "1" && district_1.value == "" && group_1.value == "") {
      ElMessage.error("区组号信息不能为空");
      return;
    } else if (district_1.value != "" && group_1.value != "") {
      tags.value.push(district_1.value + "区" + group_1.value + "组");
      scenModel.data.operateInfo.push({
        code: typeOpt.value,
        deviceName: pid.value,
        area: district_1.value,
        address: address,
        action: "callScene",
        params: transferScene.value,
        identity: identity_1.value,
      });
      lampVisible.value = false;
    }
  } else {
    address = num_1.value;
    if (typeOpt.value == "1" && district_1.value == "" && num_1.value == "") {
      ElMessage.error("区组号信息不能为空");
      return;
    } else if (district_1.value != "" && num_1.value != "") {
      tags.value.push(district_1.value + "区" + num_1.value + "号");
      scenModel.data.operateInfo.push({
        code: typeOpt.value,
        deviceName: pid.value,
        area: district_1.value,
        address: address,
        action: "callScene",
        params: transferScene.value,
        identity: identity_1.value,
      });
      lampVisible.value = false;
    }
  }
};

const enterAdd = () => {
  let arr = JSON.parse(JSON.stringify(multipleSelection2.value));
  for (let i = 0; i < arr.length; i++) {
    arr[i].action = 1;
  }
  let params = {
    deviceFid: pid.value,
    operationDevices: arr,
  };
  $axios.post("/operationDevice/updateBatch", params).then((res) => {
    if (res.data.code == 200) {
      layer.msg("新增成功！");
      dialogVisible2.value = false;
      getList({ deviceFid: pid.value });
    }
  });
};
const close = () => {
  dialogVisible2.value = false;
  window.clearInterval(timer);
};
const allDarkenDelay = () => {
  slider.render({
    elem: "#slideTest1", //绑定元素
    input: true, //输入框
    min: 0,
    change: function (value) {
      lightNum = value;
    },
  });
  mainIndex = layer.open({
    type: 1,
    title: "全部关灯延时",
    content: $("#slideDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      let params = {
        deviceFid: pid.value,
        mesh: {
          code: 400,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: "setDelayTime",
          params: lightNum,
          identity: "",
        },
      };
      params.mesh.code = area.value;
      params.mesh.area = district.value;
      if (area.value == 400) {
        params.mesh.address = district.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
      }
      if (area.value == 100) {
        params.mesh.address = num.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (num.value === "") {
          ElMessage.error("号不能为空");
          return;
        }
      }
      if (area.value == 200) {
        params.mesh.address = group.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (group.value === "") {
          ElMessage.error("组不能为空");
          return;
        }
      }
      if (area.value == 300) {
        params.mesh.address = tag.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (tag.value == "") {
          ElMessage.error("标签不能为空");
          return;
        }
      }
      layer.close(mainIndex);
      loading = showLoad("全部关灯延时请求中...");
      $axios.post("/operation/mesh", params).then((res) => {
        if (res.data.code === 200) {
          layer.msg("全部关灯延时请求成功！");
        } else {
          layer.msg("全部关灯延时调整失败!");
        }
      });

      let params2 = {
        deviceFid: pid.value,
        mesh: {
          code: 400,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: "setDelayMode",
          params: "一段",
          identity: "",
        },
      };
      $axios.post("/operation/mesh", params2).then((res) => {
        if (res.data.code === 200) {
          layer.msg("全部关灯延时请求成功！");
        } else {
          layer.msg("全部关灯延时调整失败!");
        }
      });
    },
    success: function (index) { },
  });
};
const allLightTest = () => {
  mainIndex = layer.open({
    type: 1,
    title: "全灯测试",
    content: $("#lightTestDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      layer.close(mainIndex);
      const lTestState = $('input[name="lTest"]:checked').val();
      let params = {
        deviceFid: pid.value,
        mesh: {
          code: 400,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: "blink",
          params: "",
          identity: "",
        },
      };
      params.mesh.code = area.value;
      params.mesh.area = district.value;
      if (area.value == 400) {
        params.mesh.address = district.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
      }
      if (area.value == 100) {
        params.mesh.address = num.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (num.value === "") {
          ElMessage.error("号不能为空");
          return;
        }
      }
      if (area.value == 200) {
        params.mesh.address = group.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (group.value === "") {
          ElMessage.error("组不能为空");
          return;
        }
      }
      if (area.value == 300) {
        params.mesh.address = tag.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (tag.value == "") {
          ElMessage.error("标签不能为空");
          return;
        }
      }
      loading = showLoad("全灯测试请求中...");
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          if (res.data.code === 200) {
            //  loading = showLoad("全灯测试请求成功！");
            layer.msg("全灯测试请求成功！");
          } else {
            layer.msg("全灯测试失败!");
          }
          data.loading = false;
        })
        .catch((err) => {
          layer.msg("全灯测试失败!");
        });
    },
    success: function (index) { },
  });
};

const gradually = () => {
  let _sensitivity = 0;
  slider.render({
    elem: "#slide3", //绑定元素
    input: true, //输入框
    min: 0,
    max: 9,
    change: function (value) {
      _sensitivity = value;
    },
  });
  let _sensitivity2 = 0;
  slider.render({
    elem: "#slide4", //绑定元素
    input: true, //输入框
    min: 0,
    max: 9,
    change: function (value) {
      _sensitivity2 = value;
    },
  });
  mainIndex = layer.open({
    type: 1,
    title: "全渐亮灭",
    content: $("#slideDiv3"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      params.mesh.code = area.value;
      params.mesh.area = district.value;
      if (area.value == 400) {
        params.mesh.address = district.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
      }
      if (area.value == 100) {
        params.mesh.address = num.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (num.value === "") {
          ElMessage.error("号不能为空");
          return;
        }
      }
      if (area.value == 200) {
        params.mesh.address = group.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (group.value === "") {
          ElMessage.error("组不能为空");
          return;
        }
      }
      if (area.value == 300) {
        params.mesh.address = tag.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (tag.value == "") {
          ElMessage.error("标签不能为空");
          return;
        }
      }
      layer.close(mainIndex);
      let sendMsg = "FFFFFFFFFFFF";
      let len = 1;
      let msg = "";
      if (11 < _sensitivity || _sensitivity < 0) {
        return layer.msg("只接受0~11档位");
      }
      msg = _sensitivity.toString(16).toUpperCase().padStart(2, "0") + len.toString(16).toUpperCase().padStart(2, "0") + sendMsg;
      let params = {
        deviceFid: pid.value,
        mesh: {
          code: 400,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: "setBrightRiseTime",
          params: _sensitivity,
          identity: "",
        },
      };
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          data.loading = false;
          if (res.data.code === 200) {
            layer.msg(`全渐亮灭请求发送成功`);
          } else {
            showLoad(`全渐亮灭发送失败`, 1000);
          }
        })
        .catch((err) => { });
      let params2 = {
        deviceFid: pid.value,
        mesh: {
          code: 400,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: "setBrightFallTime",
          params: _sensitivity2,
          identity: "",
        },
      };
      $axios
        .post("/operation/mesh", params2)
        .then((res) => {
          data.loading = false;
          if (res.data.code === 200) {
            layer.msg(`全渐亮灭请求发送成功`);
          } else {
            showLoad(`全渐亮灭发送失败`, 1000);
          }
        })
        .catch((err) => { });
    },
  });
};

const allSensitivity = () => {
  let _sensitivity = 0;
  slider.render({
    elem: "#slideTest1", //绑定元素
    input: true, //输入框
    min: 0,
    max: 11,
    change: function (value) {
      _sensitivity = value;
    },
  });
  mainIndex = layer.open({
    type: 1,
    title: "调整感应头灵敏度",
    content: $("#slideDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      params.mesh.code = area.value;
      params.mesh.area = district.value;
      if (area.value == 400) {
        params.mesh.address = district.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
      }
      if (area.value == 100) {
        params.mesh.address = num.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (num.value === "") {
          ElMessage.error("号不能为空");
          return;
        }
      }
      if (area.value == 200) {
        params.mesh.address = group.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (group.value === "") {
          ElMessage.error("组不能为空");
          return;
        }
      }
      if (area.value == 300) {
        params.mesh.address = tag.value;
        if (district.value === "") {
          ElMessage.error("区不能为空");
          return;
        }
        if (tag.value == "") {
          ElMessage.error("标签不能为空");
          return;
        }
      }
      layer.close(mainIndex);
      let sendMsg = "FFFFFFFFFFFF";
      let len = 1;
      let msg = "";
      if (11 < _sensitivity || _sensitivity < 0) {
        return layer.msg("只接受0~11档位");
      }
      msg = _sensitivity.toString(16).toUpperCase().padStart(2, "0") + len.toString(16).toUpperCase().padStart(2, "0") + sendMsg;
      sensitivity(0, msg, _sensitivity);
    },
  });
};
const allPropertie = () => {
  layer.msg(`正在设置中....`);
  mainIndex = layer.open({
    type: 1,
    title: "全设备属性",
    content: $("#lightTestDiv5"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      let params2 = {
        deviceFid: pid.value,
        mesh: {
          code: 400,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: radio4.value,
          params: "",
          identity: "",
        },
      };
      $axios
        .post("/operation/mesh", params2)
        .then((res) => {
          if (res.data.code === 200) {
            layer.msg(`设置成功`);
            layer.close(mainIndex);
          } else {
            showLoad(`设置失败`, 1000);
            layer.close(mainIndex);
          }
        })
        .catch((err) => { });
    },
  });
  return;
};
let dialogVisible2 = ref(false);
let timer = null;
const newAdd = () => {
  data.loading2 = true;
  dialogVisible2.value = true;
  timer = window.setInterval(function () {
    $axios
      .post("/operationDevice/scan", {
        deviceFid: pid.value,
      })
      .then((res) => {
        data.tableData2 = res.data.data;
      });
  }, 1000);
};

const lightOn = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  operationalType("lightOn", 0, false);
};

const adjustLight = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  var _sensitivity = 0;
  slider.render({
    elem: "#slideTest1", //绑定元素
    input: true, //输入框
    min: 0,
    change: function (value) {
      _sensitivity = value;
    },
  });
  mainIndex = layer.open({
    type: 1,
    title: "调整设备亮度",
    content: $("#slideDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      layer.close(mainIndex);
      for (let i = 0; i < multipleSelection.value.length; i++) {
        let item = multipleSelection.value[i];
        let macs = (item.deviceIndex * 1).toString(16).toUpperCase().toString();
        if (macs.length == 2) {
          macs = "00" + macs;
        }
        let address = macs[0] + macs[1] + " " + macs[2] + macs[3];
        let params = {
          deviceFid: pid.value,
          mesh: {
            code: 100,
            deviceName: pid.value,
            area: item.areaCode,
            address: address,
            action: "setHighBright",
            params: _sensitivity,
            identity: "",
          },
        };
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              layer.msg(`调光请求发送成功`);
            } else {
              showLoad(`调光请求发送失败`, 1000);
            }
          })
          .catch((err) => { });
      }
    },
  });
};
const adjustColor = () => {
  var _sensitivity = 0;
  slider.render({
    elem: "#slideTest1", //绑定元素
    input: true, //输入框
    min: 0,
    change: function (value) {
      _sensitivity = value;
    },
  });
  mainIndex = layer.open({
    type: 1,
    title: "调整设备色温",
    content: $("#slideDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      layer.close(mainIndex);
      for (let i = 0; i < multipleSelection.value.length; i++) {
        let item = multipleSelection.value[i];
        let macs = (item.deviceIndex * 1).toString(16).toUpperCase().toString();
        if (macs.length == 2) {
          macs = "00" + macs;
        }
        let address = macs[0] + macs[1] + " " + macs[2] + macs[3];
        let params = {
          deviceFid: pid.value,
          mesh: {
            code: 100,
            deviceName: pid.value,
            area: item.areaCode,
            address: address,
            action: "setCctBright",
            params: _sensitivity,
            identity: "",
          },
        };
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              layer.msg(`调色请求发送成功`);
            } else {
              showLoad(`调色请求发送失败`, 1000);
            }
          })
          .catch((err) => { });
      }
    },
  });
};

const setLightNum = () => {
  let lightNum1 = 0;
  let lightNum2 = 0;
  let ins1 = slider1.render({
    elem: "#slide1", //绑定元素
    input: true, //输入框
    min: 0,
    max: 100,
    value: lightOffNum,
    change: function (value) {
      lightNum1 = value;
    },
  });
  let ins2 = slider2.render({
    elem: "#slide2", //绑定元素
    input: true, //输入框
    min: 10,
    max: 100,
    value: lightOnNum,
    change: function (value) {
      lightNum2 = value;
    },
  });
  if (lightOnNum !== 0) {
    lightNum1 = lightOffNum;
    lightNum2 = lightOnNum;
  } else {
    lightNum1 = 0;
    lightNum2 = 20;
  }
  mainIndex = layer.open({
    type: 1,
    title: "设置开关灯值:",
    content: $("#slideDiv2"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      $.post(
        $httpUrl + "/userLightNum/setUserLightNum",
        {
          deviceFid: pid.value,
          lightOnNum: lightNum2,
          lightOffNum: lightNum1,
        },
        function (res) {
          if (res.code === 200) {
            layer.msg("设置成功！");
            layer.close(mainIndex);
            lightOffNum = lightNum1;
            lightOnNum = lightNum2;
            let params = {
              deviceFid: pid.value,
              mesh: {
                code: 400,
                deviceName: pid.value,
                area: "00 00",
                address: "00 00",
                action: "setHighBright",
                params: lightNum2,
                identity: "",
              },
            };
            $axios
              .post("/operation/mesh", params)
              .then((res) => {
                data.loading = false;
              })
              .catch((err) => { });
            let params2 = {
              deviceFid: pid.value,
              mesh: {
                code: 400,
                deviceName: pid.value,
                area: "00 00",
                address: "00 00",
                action: "setStandbyBright",
                params: lightNum1,
                identity: "",
              },
            };
            $axios
              .post("/operation/mesh", params2)
              .then((res) => {
                data.loading = false;
              })
              .catch((err) => { });
          } else {
            layer.msg("设置失败！");
          }
        }
      );
    },
  });
};

const setsensitivity = () => {
  var _sensitivity = 0;
  let sendMsg1 = "";
  let len = 0;
  let msg = "";
  for (let i in multipleSelection.value) {
    sendMsg1 += multipleSelection.value[i].macaddress;
    len += 1;
  }
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  slider.render({
    elem: "#slideTest1", //绑定元素
    input: true, //输入框
    min: 0,
    max: 11,
    change: function (value) {
      _sensitivity = value;
    },
  });
  mainIndex = layer.open({
    type: 1,
    title: "调整感应头灵敏度",
    content: $("#slideDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      layer.close(mainIndex);
      if (11 < _sensitivity || _sensitivity < 0) {
        return layer.msg("只接受0~11档位");
      }
      msg = _sensitivity.toString(16).toUpperCase().padStart(2, "0") + len.toString(16).toUpperCase().padStart(2, "0") + sendMsg1;
      sensitivity(1, msg);
    },
  });
};
const lightTest = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  layer.msg("正在请求中....");
  mainIndex = layer.open({
    type: 1,
    title: "单灯测试",
    content: $("#lightTestDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      layer.close(mainIndex);
      let lTestState = $('input[name="lTest"]:checked').val();
      if (lTestState === "0") {
        for (let i = 0; i < multipleSelection.value.length; i++) {
          let item = multipleSelection.value[i];
          let macs = (item.deviceIndex * 1).toString(16).toUpperCase().toString();
          if (macs.length == 2) {
            macs = "00" + macs;
          }
          let address = macs[0] + macs[1] + " " + macs[2] + macs[3];
          let params = {
            deviceFid: pid.value,
            mesh: {
              code: 100,
              deviceName: pid.value,
              area: item.areaCode,
              address: address,
              action: "blink",
              params: "",
              identity: "",
            },
          };
          $axios
            .post("/operation/mesh", params)
            .then((res) => {
              /// layer.msg("请求成功!");
              data.loading = false;
              if (res.data.code === 200) {
                layer.msg(`单灯测试请求发送成功`);
              } else {
                showLoad(`单灯测试请求发送失败`, 1000);
              }
            })
            .catch((err) => { });
        }
      } else {
        for (let i = 0; i < multipleSelection.value.length; i++) {
          let item = multipleSelection.value[i];
          let macs = (item.deviceIndex * 1).toString(16).toUpperCase().toString();
          if (macs.length == 2) {
            macs = "00" + macs;
          }
          let address = macs[0] + macs[1] + " " + macs[2] + macs[3];
          let params = {
            deviceFid: pid.value,
            mesh: {
              code: 100,
              deviceName: pid.value,
              area: item.areaCode,
              address: address,
              action: "stopBlink",
              params: "",
              identity: "",
            },
          };
          $axios
            .post("/operation/mesh", params)
            .then((res) => {
              data.loading = false;
              if (res.data.code === 200) {
                layer.msg(`单灯测试请求发送成功`);
              } else {
                showLoad(`单灯测试请求发送失败`, 1000);
              }
            })
            .catch((err) => { });
        }
      }
    },
  });
};

const sensitivity = (operationalType, msg, _sensitivity) => {
  let typeList = ["感应头灵敏度", "设置灵敏度"];
  loading = showLoad(`${typeList[operationalType]}请求中...`);
  let params = {
    deviceFid: pid.value,
    mesh: {
      code: 400,
      deviceName: pid.value,
      area: "00 00",
      address: "00 00",
      action: "setSensorIntrval",
      params: _sensitivity,
      identity: "",
    },
  };
  $axios.post("/operation/mesh", params).then((res) => {
    if (res.data.code === 200) {
      layer.msg("灵敏度设置请求成功！");
    } else {
      layer.msg("灵敏度设置请求失败!");
    }
  });
};

const lightOff = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  operationalType("lightOff", 2, false);
};
const setArea = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  layer.msg(`正在设置中....`);
  mainIndex = layer.open({
    type: 1,
    title: "当前设备:",
    content: $("#lightNameDiv4"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      const areaCode = $("#areaCode").val();
      let load = layer.msg("正在设置中...", {
        icon: 16,
        shade: [0.5, "#f5f5f5"],
        time: 0,
        scrollbar: false,
        offset: "auto",
      });
      let arr = JSON.parse(JSON.stringify(multipleSelection.value));
      for (let i = 0; i < arr.length; i++) {
        arr[i].areaCode = areaCode;
      }
      let params = {
        deviceFid: pid.value,
        operationDevices: arr,
      };
      let param = {
        deviceFid: pid.value,
        mesh: {
          code: 400,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: "setAreaAddress",
          params: areaCode,
          identity: "",
        },
      };
      $axios.post("/operation/mesh", param).then((res) => {
        $axios.post("/operationDevice/updateBatch", params).then((res) => {
          if (res.data.code === 200) {
            layer.msg("设置成功！");
            layer.close(mainIndex);
            getList({ deviceFid: pid.value });
          } else {
            layer.msg("设置失败!");
            layer.close(mainIndex);
          }
        });
      });
    },
  });
};

const groupNum = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }

  mainIndex = layer.open({
    type: 1,
    title: "当前设备:",
    content: $("#lightNameDiv2"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      const groupNum = $("#groupNum").val();
      let sendMsg = "";
      let len = 0;
      let msg = "";
      for (let i in multipleSelection.value) {
        sendMsg += multipleSelection.value[i].macaddress;
        len += 1;
      }

      if (groupNum) {
        if (!/^[0-9a-fA-F]{1,2}$/.test(groupNum)) {
          return layer.msg("组号不合法,应为2位16进制");
        }
        msg = groupNum.toUpperCase().padStart(2, "0") + "02" + len.toString(16).toUpperCase().padStart(2, "0") + sendMsg;
      }
      let load = layer.msg("正在设置中...", {
        icon: 16,
        shade: [0.5, "#f5f5f5"],
        time: 0,
        scrollbar: false,
        offset: "auto",
      });
      let arr = JSON.parse(JSON.stringify(multipleSelection.value));
      for (let i = 0; i < arr.length; i++) {
        arr[i].groupName = groupNum;
      }
      let param = {
        deviceFid: pid.value,
        mesh: {
          code: 200,
          deviceName: pid.value,
          area: "00 00",
          address: "00 00",
          action: "setClusterAddress",
          params: groupNum,
          identity: "",
        },
      };
      $axios.post("/operation/mesh", param).then((res) => {
        let obj = {
          deviceFid: pid.value,
          operationDevices: arr,
        };
        $axios.post("/operationDevice/updateBatch", obj).then((res) => {
          if (res.data.code === 200) {
            layer.msg("设置成功！");
            layer.close(mainIndex);
            getList({ deviceFid: pid.value });
          } else {
            layer.msg("设置失败!");
            layer.close(mainIndex);
          }
        });
      });
    },
  });
};

const ungroup = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  const groupNum = "00";
  let sendMsg = "";
  let len = 0;
  let msg = "";
  for (let i in multipleSelection.value) {
    sendMsg += multipleSelection.value[i].macaddress;
    len += 1;
  }
  if (groupNum) {
    if (!/^[0-9a-fA-F]{1,2}$/.test(groupNum)) {
      return layer.msg("组号不合法,应为2位16进制");
    }
    msg = groupNum.toUpperCase().padStart(2, "0") + "00" + len.toString(16).toUpperCase().padStart(2, "0") + sendMsg;
  }
  let load = layer.msg("正在解除中...", {
    icon: 16,
    shade: [0.5, "#f5f5f5"],
    time: 0,
    scrollbar: false,
    offset: "auto",
  });
  let arr = JSON.parse(JSON.stringify(multipleSelection.value));
  for (let i = 0; i < arr.length; i++) {
    arr[i].groupName = "00";
  }
  let params = { deviceFid: pid.value, operationDevices: arr };
  $axios.post("/operationDevice/updateBatch", params).then((res) => {
    if (res.data.code === 200) {
      layer.msg("解除成功！");
      layer.close(mainIndex);
      getList({ deviceFid: pid.value });
    } else {
      layer.msg("解除失败!");
      layer.close(mainIndex);
    }
  });
};
const unbind = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  let arr = JSON.parse(JSON.stringify(multipleSelection.value));
  for (let i = 0; i < arr.length; i++) {
    arr[i].action = 0;
  }
  let params = { deviceFid: pid.value, operationDevices: arr };
  $axios.post("/operationDevice/updateBatch", params).then((res) => {
    if (res.data.code === 200) {
      layer.msg("解除成功！");
      layer.close(mainIndex);
      getList({ deviceFid: pid.value });
    } else {
      layer.msg("解除失败!");
      layer.close(mainIndex);
    }
  });
};
const updateCustomName = () => {
  let ls = multipleSelection.value.map((item) => {
    return item.id;
  });
  if (ls.length == 0) {
    layer.msg("还未选择设备！");
    return;
  }
  layer.msg(`正在设置中....`);
  mainIndex = layer.open({
    type: 1,
    title: "当前设备:",
    content: $("#lightNameDiv"),
    area: ["40%", "35%"],
    btnAlign: "c",
    btn: ['<div class="layui-icon layui-icon-release">确定</div>', '<div class="layui-icon layui-icon-close">取消</div>'],
    yes: function (index, layero) {
      const customName = $("#customName").val();
      let arr = JSON.parse(JSON.stringify(multipleSelection.value));
      for (let i = 0; i < arr.length; i++) {
        arr[i].customName = customName;
      }
      let params = {
        deviceFid: pid.value,
        operationDevices: arr,
      };
      $axios.post("/operationDevice/updateBatch", params).then((res) => {
        if (res.data.code === 200) {
          layer.msg("设置成功！");
          layer.close(mainIndex);
          getList({ deviceFid: pid.value });
          getDeviceAliasAllList();
        } else {
          layer.msg("设置失败!");
          layer.close(mainIndex);
        }
      });
    },
  });
};
let area = ref("");
let gateway = ref("");
let darrOptions = ref([
  { label: i18n.language === "zh-cn" ? "场所灯操作" : "Placelightoperation", value: "" },
  { label: i18n.language === "zh-cn" ? "区操作" : "Zoneoperation", value: "400" },
  { label: i18n.language === "zh-cn" ? "单灯操作" : "Singlelampoperation", value: "100" },
  { label: i18n.language === "zh-cn" ? "群组操作" : "Groupoperation", value: "200" },
  { label: i18n.language === "zh-cn" ? "标签操作" : "Labeloperation", value: "300" },
]);
let sliderValue = ref(100);
let sliderValue2 = ref(0);
let sliderValue3 = ref(100);
let sliderValue4 = ref(1);
let dateOptions = ref([
  {
    value: "0",
    label: "0",
  },
  {
    value: "5",
    label: "5",
  },
  {
    value: "10",
    label: "10",
  },
  {
    value: "20",
    label: "20",
  },
  {
    value: "60",
    label: "60",
  },
  {
    value: "120",
    label: "120",
  },
  {
    value: "300",
    label: "300",
  },
  {
    value: "600",
    label: "600",
  },
  {
    value: "900",
    label: "900",
  },
  {
    value: "1800",
    label: "1800",
  },
]);
let switchValue = ref(false);
let switchValue2 = ref(false);
let oneDate = ref(0);
let one2Date = ref(0);
let sliderValue5 = ref(1);
let sliderValue6 = ref(3);
let sliderValue7 = ref(1);
let sliderValue8 = ref(3);
let switchValue3 = ref(false);
let input1 = ref("");
let switchValue4 = ref(false);
let sliderValue9 = ref(1);
let input2 = ref("");
let sliderValue10 = ref(3);
let sliderValue11 = ref(3);
let sliderValue12 = ref(3);
let switchValue5 = ref(false);
let input3 = ref("");
let input4 = ref("");
let input5 = ref("");
let input6 = ref("");
let input7 = ref("");
let input8 = ref("");
let input9 = ref("");
let input10 = ref("");
let input11 = ref("");
let input12 = ref("");
let input13 = ref("");
let identity = ref("");
const sceneTableData = ref([]);
let gateway_1 = ref("");
let district = ref("");
let tag = ref("");
let group = ref("");
let num = ref("");
let scene = ref("");
let identity_1 = ref("");
let sceneOptions = ref([]);
onBeforeMount(() => {
  console.log("wirelessdevice->组件挂载前->", sceneOptions);
  let arr = [];
  for (let index = 1; index < 13; index++) {
    let obj = { value: index, label: `场景${index}` };
    arr.push(obj);
  }
  sceneOptions.value = arr;
});
let carryOptions = ref([
  { value: "1,2,3,4,5,6,7", label: "每天执行" },
  { value: "1", label: "每周一执行" },
  { value: "2", label: "每周二执行" },
  { value: "3", label: "每周三执行" },
  { value: "4", label: "每周四执行" },
  { value: "5", label: "每周五执行" },
  { value: "6", label: "每周六执行" },
  { value: "7", label: "每周日执行" },
]);
let carryvalue = ref("");
let transferScene = ref("");
let updateScene = ref("");
let sceneVisible = ref(true);
let timeValue = ref();
let isEdit = false;

let typeOptions = ref([
  { value: "400", label: "新增整区" },
  { value: "200", label: "新增整组" },
  { value: "100", label: "新增单号" },
  { value: "10", label: "自由新增" },
]);
let typeOpt = ref("400");
const addScene = () => {
  sceneVisible.value = !sceneVisible.value;
  scenModel.data = {
    chooseWorkDay: "1,2,3,4,5,6,7",
    startTime: "08:00",
    operateInfo: [],
  };
  carryvalue.value = "";
  transferScene.value = 1;
  isEdit = true;
};
let lampVisible = ref(false);
const addingLamp = () => {
  lampVisible.value = true;
};
const deleteTag = (i) => {
  ElMessageBox.confirm("是否删除此条记录?", "提示").then(() => {
    scenModel.data.operateInfo.splice(i, 1);
  });
};
const send = (action, value) => {
  let params = {
    deviceFid: pid.value,
    mesh: {
      code: area.value,
      deviceName: pid.value,
      area: district.value,
      address: "00 00",
      action: action,
      params: value,
      identity: identity.value,
    },
  };
  if (area.value == 400) {
    params.mesh.address = district.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
  }
  if (area.value == 100) {
    params.mesh.address = num.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (num.value === "") {
      ElMessage.error("号不能为空");
      return;
    }
  }
  if (area.value == 200) {
    params.mesh.address = group.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (group.value === "") {
      ElMessage.error("组不能为空");
      return;
    }
  }
  if (area.value == 300) {
    params.mesh.address = tag.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (tag.value == "") {
      ElMessage.error("标签不能为空");
      return;
    }
  }
  if (area.value == "") {
    params.mesh.area = "00 00";
    params.mesh.code = "400";
  }
  layer.msg("正在请求中....", { time: 0 });
  if (gateway.value == "" && area.value != "") {
    for (let i = 0; i < gatewayList.value.length; i++) {
      params.mesh.deviceName = gatewayList.value[i].id;
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          data.loading = false;
          if (res.data.code === 200) {
            layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
          } else {
            showLoad(`请求发送失败`, 500);
          }
        })
        .catch((err) => { });
    }
  } else {
    $axios
      .post("/operation/mesh", params)
      .then((res) => {
        data.loading = false;
        if (res.data.code === 200) {
          layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
        } else {
          showLoad(`请求发送失败`, 500);
        }
      })
      .catch((err) => { });
  }
};

const Everbright = (action) => {
  let params = {
    deviceFid: pid.value,
    mesh: {
      code: area.value,
      deviceName: pid.value,
      area: district.value,
      address: "00 00",
      action: action,
      params: "",
      identity: identity.value,
    },
  };
  if (area.value == 400) {
    params.mesh.address = district.value;

    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
  }
  if (area.value == 100) {
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (num.value === "") {
      ElMessage.error("号不能为空");
      return;
    }
    if (multipleSelection.value.length > 0) {
      params.mesh.address = multipleSelection.value[0].deviceIndex;
    } else {
      params.mesh.address = num.value;
    }
  }
  if (area.value == 200) {
    params.mesh.address = group.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (group.value === "") {
      ElMessage.error("组不能为空");
      return;
    }
  }
  if (area.value == 300) {
    params.mesh.address = tag.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (tag.value == "") {
      ElMessage.error("标签不能为空");
      return;
    }
  }
  if (area.value == "") {
    params.mesh.area = "00 00";
    params.mesh.code = "400";
  }
  if (gateway.value == "" && area.value != "") {
    for (let i = 0; i < gatewayList.value.length; i++) {
      params.mesh.deviceName = gatewayList.value[i].id;
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          data.loading = false;
          if (res.data.code === 200) {
            layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
          } else {
            showLoad(`请求发送失败`, 500);
          }
        })
        .catch((err) => { });
    }
  } else if (gateway.value != "" && area.value != "") {
    params.mesh.deviceName = gateway.value;
    $axios
      .post("/operation/mesh", params)
      .then((res) => {
        data.loading = false;
        if (res.data.code === 200) {
          layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
        } else {
          showLoad(`请求发送失败`, 500);
        }
      })
      .catch((err) => { });
  } else {
    if (area.value == "" && multipleSelection.value.length == 0) {
      for (let i = 0; i < deviceAreaAllList.value.length; i++) {
        params.mesh.area = deviceAreaAllList.value[i].area_code;
        params.mesh.address = deviceAreaAllList.value[i].area_code;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              if (i == deviceAreaAllList.value.length - 1) {
                layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
              }
            } else {
              if (i == deviceAreaAllList.value.length - 1) {
                showLoad(`请求发送失败`, 500);
              }
            }
          })
          .catch((err) => { });
      }
    } else if (area.value == "" && multipleSelection.value.length != 0) {
      for (let i = 0; i < multipleSelection.value.length; i++) {
        let address = multipleSelection.value[i].deviceIndex;
        params.mesh.address = address;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              layer.msg(`请求发送成功`, { time: 500 });
            } else {
              showLoad(`请求发送失败`, 500);
            }
          })
          .catch((err) => { });
      }
    }
  }
};
const extinction = (action) => {
  let params = {
    deviceFid: pid.value,
    mesh: {
      code: area.value,
      deviceName: pid.value,
      area: district.value,
      address: "00 00",
      action: action,
      params: "",
      identity: identity.value,
    },
  };

  if (area.value == 400) {
    params.mesh.address = district.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
  }

  if (area.value == 100) {
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (num.value === "") {
      ElMessage.error("号不能为空");
      return;
    }
    if (multipleSelection.value.length > 0) {
      params.mesh.address = multipleSelection.value[0].deviceIndex;
    } else {
      params.mesh.address = num.value;
    }
  }
  if (area.value == 200) {
    params.mesh.address = group.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (group.value === "") {
      ElMessage.error("组不能为空");
      return;
    }
  }
  if (area.value == 300) {
    params.mesh.address = tag.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (tag.value == "") {
      ElMessage.error("标签不能为空");
      return;
    }
  }
  if (area.value == "") {
    params.mesh.area = "00 00";
    params.mesh.code = "400";
  }
  if (gateway.value == "" && area.value != "") {
    for (let i = 0; i < gatewayList.value.length; i++) {
      params.mesh.deviceName = gatewayList.value[i].id;
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          data.loading = false;
          if (res.data.code === 200) {
            layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
          } else {
            showLoad(`请求发送失败`, 500);
          }
        })
        .catch((err) => { });
    }
  } else if (gateway.value != "" && area.value != "") {
    params.mesh.deviceName = gateway.value;
    $axios
      .post("/operation/mesh", params)
      .then((res) => {
        data.loading = false;
        if (res.data.code === 200) {
          layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
        } else {
          showLoad(`请求发送失败`, 500);
        }
      })
      .catch((err) => { });
  } else {
    if (area.value == "" && multipleSelection.value.length == 0) {
      for (let i = 0; i < deviceAreaAllList.value.length; i++) {
        params.mesh.area = deviceAreaAllList.value[i].area_code;
        params.mesh.address = deviceAreaAllList.value[i].area_code;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              if (i == deviceAreaAllList.value.length - 1) {
                layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
              }
            } else {
              if (i == deviceAreaAllList.value.length - 1) {
                showLoad(`请求发送失败`, 500);
              }
            }
          })
          .catch((err) => { });
      }
    } else if (area.value == "" && multipleSelection.value.length != 0) {
      for (let i = 0; i < multipleSelection.value.length; i++) {
        let address = multipleSelection.value[i].deviceIndex;
        params.mesh.address = address;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              layer.msg(`请求发送成功`, { time: 500 });
            } else {
              showLoad(`请求发送失败`, 500);
            }
          })
          .catch((err) => { });
      }
    }
  }
};
const flash = (action) => {
  let params = {
    deviceFid: pid.value,
    mesh: {
      code: area.value,
      deviceName: pid.value,
      area: district.value,
      address: "00 00",
      action: action,
      params: "",
      identity: identity.value,
    },
  };
  if (area.value == 400) {
    params.mesh.address = district.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
  }
  if (area.value == 100) {
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (num.value === "") {
      ElMessage.error("号不能为空");
      return;
    }
    params.mesh.address = multipleSelection.value[0].deviceIndex;
  }
  if (area.value == 200) {
    params.mesh.address = group.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (group.value === "") {
      ElMessage.error("组不能为空");
      return;
    }
  }
  if (area.value == 300) {
    params.mesh.address = tag.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (tag.value == "") {
      ElMessage.error("标签不能为空");
      return;
    }
  }
  if (area.value == "") {
    params.mesh.area = "00 00";
    params.mesh.code = "400";
  }
  if (gateway.value == "" && area.value != "") {
    for (let i = 0; i < gatewayList.value.length; i++) {
      params.mesh.deviceName = gatewayList.value[i].id;
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          data.loading = false;
          if (res.data.code === 200) {
            layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
          } else {
            showLoad(`请求发送失败`, 500);
          }
        })
        .catch((err) => { });
    }
  } else if (gateway.value != "" && area.value != "") {
    params.mesh.deviceName = gateway.value;
    $axios
      .post("/operation/mesh", params)
      .then((res) => {
        data.loading = false;
        if (res.data.code === 200) {
          layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
        } else {
          showLoad(`请求发送失败`, 500);
        }
      })
      .catch((err) => { });
  } else {
    if (area.value == "" && multipleSelection.value.length == 0) {
      for (let i = 0; i < deviceAreaAllList.value.length; i++) {
        params.mesh.area = deviceAreaAllList.value[i].area_code;
        params.mesh.address = deviceAreaAllList.value[i].area_code;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              if (i == deviceAreaAllList.value.length - 1) {
                layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
              }
            } else {
              if (i == deviceAreaAllList.value.length - 1) {
                showLoad(`请求发送失败`, 500);
              }
            }
          })
          .catch((err) => { });
      }
    } else if (area.value == "" && multipleSelection.value.length != 0) {
      for (let i = 0; i < multipleSelection.value.length; i++) {
        let address = multipleSelection.value[i].deviceIndex;
        params.mesh.address = address;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              layer.msg(`请求发送成功`, { time: 500 });
            } else {
              showLoad(`请求发送失败`, 500);
            }
          })
          .catch((err) => { });
      }
    }
  }
};

const stopFlash = (action) => {
  let params = {
    deviceFid: pid.value,
    mesh: {
      code: area.value,
      deviceName: pid.value,
      area: district.value,
      address: "00 00",
      action: action,
      params: "",
      identity: identity.value,
    },
  };
  if (area.value == 400) {
    params.mesh.address = district.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
  }
  if (area.value == 100) {
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (num.value === "") {
      ElMessage.error("号不能为空");
      return;
    }
    params.mesh.address = multipleSelection.value[0].deviceIndex;
  }
  if (area.value == 200) {
    params.mesh.address = group.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (group.value === "") {
      ElMessage.error("组不能为空");
      return;
    }
  }
  if (area.value == 300) {
    params.mesh.address = tag.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (tag.value == "") {
      ElMessage.error("标签不能为空");
      return;
    }
  }
  if (area.value == "") {
    params.mesh.area = "00 00";
    params.mesh.code = "400";
  }
  if (gateway.value == "" && area.value != "") {
    for (let i = 0; i < gatewayList.value.length; i++) {
      params.mesh.deviceName = gatewayList.value[i].id;
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          data.loading = false;
          if (res.data.code === 200) {
            layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
          } else {
            showLoad(`请求发送失败`, 500);
          }
        })
        .catch((err) => { });
    }
  } else if (gateway.value != "" && area.value != "") {
    params.mesh.deviceName = gateway.value;
    $axios
      .post("/operation/mesh", params)
      .then((res) => {
        data.loading = false;
        if (res.data.code === 200) {
          layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
        } else {
          showLoad(`请求发送失败`, 500);
        }
      })
      .catch((err) => { });
  } else {
    if (area.value == "" && multipleSelection.value.length == 0) {
      for (let i = 0; i < deviceAreaAllList.value.length; i++) {
        params.mesh.area = deviceAreaAllList.value[i].area_code;
        params.mesh.address = deviceAreaAllList.value[i].area_code;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              if (i == deviceAreaAllList.value.length - 1) {
                layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
              }
            } else {
              if (i == deviceAreaAllList.value.length - 1) {
                showLoad(`请求发送失败`, 500);
              }
            }
          })
          .catch((err) => { });
      }
    } else if (area.value == "" && multipleSelection.value.length != 0) {
      for (let i = 0; i < multipleSelection.value.length; i++) {
        let address = multipleSelection.value[i].deviceIndex;
        params.mesh.address = address;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
            } else {
              showLoad(`请求发送失败`, 500);
            }
          })
          .catch((err) => { });
      }
    }
  }
};

const dormancy = (action) => {
  let params = {
    deviceFid: pid.value,
    mesh: {
      code: area.value,
      deviceName: pid.value,
      area: district.value,
      address: "00 00",
      action: action,
      params: "",
      identity: identity.value,
    },
  };

  if (area.value == 400) {
    params.mesh.address = district.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
  }

  if (area.value == 100) {
    // params.mesh.address = num.value;

    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (num.value === "") {
      ElMessage.error("号不能为空");
      return;
    }
    params.mesh.address = multipleSelection.value[0].deviceIndex;
  }

  if (area.value == 200) {
    params.mesh.address = group.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (group.value === "") {
      ElMessage.error("组不能为空");
      return;
    }
  }

  if (area.value == 300) {
    params.mesh.address = tag.value;
    if (district.value === "") {
      ElMessage.error("区不能为空");
      return;
    }
    if (tag.value == "") {
      ElMessage.error("标签不能为空");
      return;
    }
  }
  if (area.value == "") {
    params.mesh.area = "00 00";
    params.mesh.code = "400";
  }
  if (gateway.value == "" && area.value != "") {
    for (let i = 0; i < gatewayList.value.length; i++) {
      params.mesh.deviceName = gatewayList.value[i].id;
      $axios
        .post("/operation/mesh", params)
        .then((res) => {
          data.loading = false;
          if (res.data.code === 200) {
            layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
          } else {
            showLoad(`请求发送失败`, 500);
          }
        })
        .catch((err) => { });
    }
  } else if (gateway.value != "" && area.value != "") {
    params.mesh.deviceName = gateway.value;
    $axios
      .post("/operation/mesh", params)
      .then((res) => {
        data.loading = false;
        if (res.data.code === 200) {
          layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
        } else {
          showLoad(`请求发送失败`, 500);
        }
      })
      .catch((err) => { });
  } else {
    if (area.value == "" && multipleSelection.value.length == 0) {
      for (let i = 0; i < deviceAreaAllList.value.length; i++) {
        params.mesh.area = deviceAreaAllList.value[i].area_code;
        params.mesh.address = deviceAreaAllList.value[i].area_code;
        // "area": district.value,
        // "address": '00 00',
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              if (i == deviceAreaAllList.value.length - 1) {
                layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
              }
            } else {
              if (i == deviceAreaAllList.value.length - 1) {
                showLoad(`请求发送失败`, 500);
              }
            }
          })
          .catch((err) => { });
      }
    } else if (area.value == "" && multipleSelection.value.length != 0) {
      for (let i = 0; i < multipleSelection.value.length; i++) {
        let address = multipleSelection.value[i].deviceIndex;
        params.mesh.address = address;
        $axios
          .post("/operation/mesh", params)
          .then((res) => {
            data.loading = false;
            if (res.data.code === 200) {
              layer.msg(i18n.language === "zh-cn" ? `请求发送成功` : "Request sent successfully", { time: 500 });
            } else {
              showLoad(`请求发送失败`, 500);
            }
          })
          .catch((err) => { });
      }
    }
  }
};

const showOper = (strt) => {
  if (strt == undefined) {
    return "";
  } else if (typeof strt === "string") {
    return JSON.parse(strt);
  } else {
    return strt;
  }
};
const changeEnableState = (row, num) => {
  let params = JSON.parse(JSON.stringify(row));
  params.enableState = num;
  params.operateInfo = JSON.stringify(params.operateInfo);

  let obj = {
    deviceFid: pid.value,
    deviceTimingRule: params,
  };

  $axios
    .post("/deviceTimingRule/update", obj)
    .then((res) => {
      if (res.data.code === 200) {
        let tips = "";
        if (num == 0) {
          tips = "停用";
        } else {
          tips = "启用";
        }
        layer.msg(tips + `成功`, { time: 500 });
        getTab2();
      }
    })
    .catch((err) => { });
};

const delScen = (row) => {
  ElMessageBox.confirm("确认删除?", "提示")
    .then(() => {
      let params = {
        id: row.id,
      };
      const formData = new FormData();
      formData.append("id", row.id);
      $axios.post("/deviceTimingRule/deleteTimingRule", formData).then((res) => {
        if (res.data.code === 200) {
          layer.msg("删除成功！");
          getTab2();
        } else {
          layer.msg("删除失败!");
        }
      });
    })
    .catch(() => { }); // 新增错误捕获
};
let scenModel = reactive({
  data: {
    chooseWorkDay: "1,2,3,4,5,6,7",
  },
});
const editScen = (row) => {
  scenModel.data = row;
  sceneVisible.value = false;
  isEdit = false;
  if (row.operateInfo.length > 0) {
    transferScene.value = row.operateInfo[0].params;
  } else {
    transferScene.value = "";
  }
};
const submitScen = () => {
  if (scenModel.data.ruleTitle == undefined || scenModel.data.ruleTitle == "") {
    ElMessage.error("任务名称不能为空！");
    return;
  }
  if (scenModel.data.operateInfo == undefined || scenModel.data.operateInfo.length == 0) {
    ElMessage.error("操作灯组不能为空！");
    return;
  }
  let obj = { deviceFid: pid.value, deviceTimingRule: scenModel.data };
  for (let i = 0; i < obj.deviceTimingRule.operateInfo.length; i++) {
    obj.deviceTimingRule.operateInfo[i].params = transferScene.value;
  }
  obj.deviceTimingRule.operateInfo = JSON.stringify(obj.deviceTimingRule.operateInfo);
  let urls = "/deviceTimingRule/update";
  if (isEdit) {
    obj.deviceTimingRule.enableState = 1;
    urls = "/deviceTimingRule/addMeshTimingRule";
  } else {
    urls = "/deviceTimingRule/update";
  }
  $axios
    .post(urls, obj)
    .then((res) => {
      if (res.data.code === 200) {
        layer.msg(`编辑成功`, { time: 500 });
        sceneVisible.value = true;
        getTab2();
      }
    })
    .catch((err) => { });
};

const showScope = (row) => {
  if (row.operateInfo && row.operateInfo.length > 0) {
    let obj = sceneOptions.value.filter((item) => {
      return row.operateInfo[0].params == item.value;
    });
    if (obj.length > 0 && obj[0].label) {
      return obj[0].label;
    }
  }
};

const showTime = (row) => {
  let obj = carryOptions.value.filter((item) => {
    return row.chooseWorkDay == item.value;
  });
  if (obj.length > 0 && obj[0].label) {
    return obj[0].label.replace("执行", "");
  }
};

const groupAndSort = (groupFields, sortFields, list) => {
  if (Array.isArray(list) && list.length <= 0) {
    return [];
  }
  let tempList = [];
  list = sortByFields(groupFields, list);
  let tempGroupValue = list[0][groupFields];
  for (let i = 0; i < list.length; i++) {
    if (!list[i]) {
      continue;
    }
    if (list[i][groupFields] != tempGroupValue) {
      tempGroupValue = list[i][groupFields];
    }
    addObjToList(groupFields, sortFields, tempGroupValue, list[i], tempList);
    list.splice(i, 1);
    i--;
  }
  return tempList;
};

const addObjToList = (groupFields, sortFields, tempGroupValue, obj, list) => {
  let tempLength = list.length;
  for (let i = 0; i < list.length; i++) {
    if (list[i][groupFields] == tempGroupValue && obj[sortFields] <= list[i][sortFields]) {
      list.splice(i, 0, obj);
      return;
    }
  }
  if (tempLength == list.length) {
    list.push(obj);
  }
};
const sortByFields = (sortFields, list) => {
  return list.sort((a, b) => {
    if (a[sortFields] < b[sortFields]) {
      return -1;
    }
    if (a[sortFields] > b[sortFields]) {
      return 1;
    }
    return 0;
  });
};

const createComprisonFunction = (propertyName) => {
  return function (object1, object2) {
    var value1 = object1[propertyName];
    var value2 = object2[propertyName];
    if (value1 < value2) {
      return -1;
    } else if (value1 > value2) {
      return 1;
    } else {
      return 0;
    }
  };
};

onMounted(() => {
  total = 0;
  layui.extend({}).use(["jquery", "layer", "form", "table", "slider", "dropdown"], function () {
    $ = layui.jquery;
    layer = layui.layer;
    form = layui.form;
    table = layui.table;
    slider = layui.slider;
    slider1 = layui.slider;
    slider2 = layui.slider;
    dropdown = layui.dropdown;
    maxNum = 3;
    setTimeouts = 2000;
    tipsTime = 1000;
    //渲染滑块
    lightNum = 0;
    slider.render({
      elem: "#slideTest1", //绑定元素
      input: true, //输入框
      min: 0,
      change: function (value) {
        lightNum = value;
      },
    });
    getDeviceAliasAllList();
    getList({});
  });
});
</script>

<style lang="scss" scoped>
.available1 {
  color: orangered;
}

:deep(.selectName .el-input__wrapper) {
  background: none !important;
  box-shadow: none !important;
}

:deep(.selectName .el-select .el-input.is-focus .el-input__wrapper) {
  box-shadow: none !important;
}

:deep(.selectName select) {
  min-width: 80px;
  margin-left: 6px;
}

:deep(.selectName ::-webkit-input-placeholder) {
  color: #fff !important;
}

:deep(.wiretab .el-checkbox__input.is-disabled .el-checkbox__inner) {
  background: #ccc;
  border-color: #ccc;
}

.dialog-footer {
  text-align: center;
  width: 100%;
  display: block;
}

:deep(.newAdd .el-table__inner-wrapper) {
  border: solid 1px #fff;
}

.newAddTab {
  border: solid 1px #fff;
  // border-bottom: 0;
}
</style>
<style lang="scss" scoped>
.newAdd .el-dialog__header {
  background: #0ec8ec69 !important;
}

.newAdd .el-dialog__header .el-dialog__title {
  color: #fff !important;
}

.el-row {
  padding: 10px 0;
  border-bottom: solid 1px #ddd;
}

.el-row>div {
  align-items: center;
}

.sliderbox {
  display: flex;
  align-items: center;
}

.sliderbox>span {
  margin-left: 20px;
  font-size: 14px;
  display: block;
  width: 30px;
}

.linev .layui-form-label {
  width: 114px;
}

.linev .layui-input-block {
  position: relative;
  top: 17px;
  margin-left: 150px;
}

:deep(.selectName input) {
  color: #fff !important;
  text-align: right;
}

:deep(.el-dialog__title) {
  color: #fff !important;
}
</style>
<style>
.newAdd .el-dialog__title {
  color: #fff !important;
}

.el-dialog__body .top .el-row>div {
  display: flex;
}

.top .el-row>div.endiv {
  display: inline;
  line-height: 260%;
}

.top .el-row>div>span {
  display: inline-block;
  text-align: right;
  padding-right: 10px;
}

.radiocont .layui-form-radio {
  display: none;
}

.btnbox .top {
  color: #fff;
}

.btnbox {
  width: 100%;
}

.tops .el-row>div>span>div {
  margin-left: 5px;
}

.newName {
  display: flex;
}

.newName>span {
  display: flex;
  width: 85px;
  text-align: right;
  font-size: 14px;
  padding-right: 10px;
  align-items: center;
  justify-content: right;
}

.newName p {
  padding-left: 30px;
}
</style>
<style>
.wirelessdevice {
  display: inline-flex !important;
}

.layui-layer-wrap .el-radio.el-radio--large .el-radio__label {
  color: #f13131 !important;
}

.top .el-select .el-input__wrapper {
  background: none;
}

.top .el-input__wrapper {
  background: none;
}

::-webkit-input-placeholder {
  color: #ccc !important;
  font-size: 16px;
}

.tops .el-input__inner {
  color: #fff !important;
}

.sceneTableData .cell {
  color: #fff;
}

.sceneTableData td .cell {
  color: #000;
}

.sceneTableData th {
  background: var(--el-color-primary) !important;
}

.sceneVisible {
  width: 100%;
}

.sceneVisible .el-col {
  margin-bottom: 10px;
}

.altcon {
  width: 120px;
  height: 36px;
  background-color: #ecf0f8;
  border-radius: 6px;
  text-align: center;
  line-height: 36px;
  margin-left: 10px;
  font-size: 14px;
  color: #1e62da;
  width: fit-content;
  padding: 0px 6px;
  margin-bottom: 10px;
  display: inline-block;
  cursor: pointer;
}

.operateInfo {
  font-weight: bold;
}

.altconp {
  padding-left: 30px;
}

.top .el-button+.el-button {
  margin-left: 6px;
}
</style>
